package com.joysuch.gui;

import com.joysuch.util.ExcelHandle;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class ExcelHandleGui extends JFrame {
    // 输出路径
    private String outPath = "C:/ExcelHandle";
    private final InputStream templeteFile = this.getClass().getResourceAsStream("/报表模板.xlsx");

    private final JButton importButton;
    private JButton choosePathButton;
    private JLabel label;

    String handleRetMsg = "Excel文件处理完成！";
    String handleRetTitle = "处理完成";

    GridBagConstraints gbc = new GridBagConstraints();
    JPanel panel = new JPanel(new GridBagLayout());

    public ExcelHandleGui() {
        setTitle("Excel导入工具");
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        importButton = createStyledButton("点击导入Excel文件", new Color(26, 158, 52));
        choosePathButton = createStyledButton("点击选择导出路径", new Color(26, 138, 151));
        label = new JLabel("输出路径:");
        choosePathButton.addActionListener(e -> setOutPath());

        long time1 = System.currentTimeMillis();
        importButton.addActionListener(e -> handleExcel());
        long time2 = System.currentTimeMillis();
        long durationSeconds = (time2 - time1) / 1000;
        System.err.println("处理时长：" + durationSeconds + "秒");

        setupLayout();
    }

    private void setupLayout() {
        gbc.insets = new Insets(5, 5, 5, 5);

        gbc.gridx = 0;
        gbc.gridy = 1;
        panel.add(choosePathButton, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(label, gbc);

        gbc.gridx = 0;
        gbc.gridy = 3;
        panel.add(new JLabel(), gbc); // Spacer

        gbc.gridx = 0;
        gbc.gridy = 4;
        panel.add(new JLabel(), gbc); // Spacer

        gbc.gridx = 0;
        gbc.gridy = 6;
        panel.add(new JLabel(), gbc); // Spacer

        gbc.gridx = 0;
        gbc.gridy = 7;
        panel.add(importButton, gbc);

        gbc.gridx = 0;
        gbc.gridy = 8;
        panel.add(new JLabel(), gbc); // Spacer

        setContentPane(panel);
    }

    private JButton createStyledButton(String text, Color bgColor) {
        JButton button = new JButton(text);
        button.setBackground(bgColor);
        button.setForeground(Color.WHITE);
        button.setFont(new Font("微软雅黑", Font.BOLD, 14));
        return button;
    }

    private void setOutPath() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            outPath = selectedFile.getAbsolutePath();
            label.setText("输出路径:" + outPath);
            panel.revalidate();
            panel.repaint();
        }
    }

    private void handleExcel() {
        JFileChooser fileChooser = new JFileChooser();
        int result = fileChooser.showOpenDialog(this);

        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            ExcelHandle.handle(selectedFile, templeteFile, outPath);
            try {
            }catch (Exception e) {
                handleRetMsg = e.getMessage();
                handleRetTitle = "处理失败";
            }
            // 处理完成后弹出提示框
            JOptionPane.showMessageDialog(this, handleRetMsg, handleRetTitle, JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ExcelHandleGui().setVisible(true));
    }
}
